/*
 *  Jajuk
 *  Copyright (C) 2003-2008 The Jajuk Team
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *  $Revision: 3132 $
 */
package aspecto;

import static org.jajuk.ui.actions.JajukAction.VIEW_RESTORE_DEFAULTS;

import com.vlsolutions.swing.docking.DockableState;

import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.*;

import org.jajuk.ui.actions.ActionBase;
import org.jajuk.ui.actions.ActionManager;
import org.jajuk.ui.actions.JajukAction;
import org.jajuk.ui.perspectives.*;
import org.jajuk.util.ITechnicalStrings;
import org.jajuk.util.IconLoader;
import org.jajuk.util.Messages;
import org.jajuk.util.Util;
import org.jajuk.util.error.JajukException;
import org.jajuk.util.log.Log;
import org.jajuk.ui.widgets.*;
/*import java.awt.event.ActionEvent;*/

import org.jajuk.ui.perspectives.IPerspective;
import org.jajuk.ui.perspectives.PerspectiveManager;
/*import org.jajuk.util.ITechnicalStrings;
import org.jajuk.util.IconLoader;
import org.jajuk.util.Messages;
import org.jajuk.util.error.JajukException;*/

/**
 * 
 */
privileged public aspect RestoreViewsAspect {
  
  /* * (non-Javadoc)
   * 
   * @see org.jajuk.ui.IPerspective#restaureDefaults()*/
  
  public void PerspectiveAdapter.restoreDefaults() {
    // SHOULD BE CALLED ONLY FOR THE CURRENT PERSPECTIVE
    // to ensure views are not invisible
    try {
      // Remove current conf file to force using default file from the
      // jar
      File loadFile = Util.getConfFileByPath(getClass().getSimpleName() + ".xml");
      loadFile.delete();
      // Remove all registered dockables
      DockableState[] ds = getDockables();
      for (int i = 0; i < ds.length; i++) {
        remove(ds[i].getDockable());
      }
      // force reload
      load();
      // set perspective again to force UI refresh
      PerspectiveManager.setCurrentPerspective(this);
    } catch (Exception e) {
      // display an error message
      Log.error(e);
      Messages.showErrorMessage(163);
    }
  }

  public abstract void IPerspective.restoreDefaults();

  pointcut installAction() :
    execution(private ActionManager.new());

  before() : installAction(){
    ActionManager.installAction(JajukAction.VIEW_RESTORE_DEFAULTS, new RestoreViewsAction(), false);
  }

  JMenuItem JajukJMenuBar.jmiRestoreDefaultViews;
  
  pointcut inicializaMenuItem(JajukJMenuBar jMenuBar,String str) :
    call(JMenu.new(String)) && this(jMenuBar) && args(str) && withincode(JajukJMenuBar.new());

  before(JajukJMenuBar jMenuBar,String str) : inicializaMenuItem(jMenuBar,str){
    if(str.equals(Messages.getString("JajukJMenuBar.25"))){
      jMenuBar.jmiRestoreDefaultViews = new JMenuItem(ActionManager.getAction(VIEW_RESTORE_DEFAULTS));
      jMenuBar.views.add(jMenuBar.jmiRestoreDefaultViews);
    }
  }  
  
  
  public class RestoreViewsAction extends ActionBase implements ITechnicalStrings {

    private static final long serialVersionUID = 1L;

    RestoreViewsAction() {
      super(Messages.getString("JajukJMenuBar.17"), IconLoader.ICON_REFRESH, true);
      setShortDescription(Messages.getString("JajukJMenuBar.17"));
    }

    public void perform(final ActionEvent e) throws JajukException {
      new Thread() {
        public void run() {
          IPerspective perspective = PerspectiveManager.getCurrentPerspective();
          // Restore local or global views
          perspective.restoreDefaults();
        }
      }.start();
    }
  }
}
